package highlight_spring4.advice;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName ExceptionHandlerAdvice
 * @Package highlight_spring4.advice
 * @Description TODO
 * @Author zjb
 * @Date 2018/7/25 12:14
 * @Version 1.0
 **/
//@ControllerAdvice声明一个控制器建言，@ControllerAdvice组合了@Component注解，所以自动注册为Sping的Bean.
@ControllerAdvice
public class ExceptionHandlerAdvice {
    //@ExceptionHandler在此处定义全局处理，通过@ExceptionHandler的value属性可过滤拦截的条件，在此处我们可以看出我们拦截所有的Exception
    @ExceptionHandler(value = Exception.class)
    public ModelAndView exception(Exception exception, WebRequest request){
        //error页面
        ModelAndView modelAndView = new ModelAndView("error");
        modelAndView.addObject("errorMessage",exception.getMessage());
        return modelAndView;
    }
    @ModelAttribute
    public void addAttributes(Model model){
        //此处使用@ModelAttribute注解将键值对添加到全局，所有注解@RequestMapping的方法可获得此键值对
        model.addAttribute("msg","额外信息");
    }
    //4
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder){
        //此处演示忽略request参数id,更多关于WebDataBinder的配置，请参考WebDataBinder的API文档。
        webDataBinder.setDisallowedFields("id");
    }
}
